package com.netty.esc.config.websocket;


//import com.netty.esc.netty.server.ServerIdleStateTrigger;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhaoyang05
 * @Data: 2022/3/1 11:12
 * @Description 处理器加载类
 */
@Component
public class WebServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    //    @Autowired
//    NettyServerHandler nettyServerHandler;
    @Autowired
    ServerIdleStateTrigger serverIdleStateTrigger;
    @Autowired
    WebSocketServerHandler webSocketServerHandler;


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("http-codec", new HttpServerCodec());
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        //心跳监听
        pipeline.addLast(new IdleStateHandler(30, 30, 30, TimeUnit.SECONDS));
        //粘包解决器
        pipeline.addLast(new StringDecoder());
        //启动工作线程组
        pipeline.addLast(WebSocketServer.workerGroup,webSocketServerHandler);
    }
}

